package com.lovo.market.common.aspect;


import com.lovo.market.common.annotation.DoesNotPackResult;
import com.lovo.market.common.model.vo.ResultVO;
import com.lovo.market.common.util.ResultFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author MirAi
 * @description 结果集处理基类
 * @date 2020/12/15 17:12
 */
public class BaseHandleResult implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {

        if (o instanceof ResultVO) {
            return o;
        }

        if (methodParameter.getContainingClass().isAnnotationPresent(DoesNotPackResult.class)) {
            return o;
        }

        if (methodParameter.getMethodAnnotation(DoesNotPackResult.class) != null) {
            return o;
        }

        return ResultFactory.success(o);
    }
}
